<?php

namespace App\Http\Controllers\Admin;

use App\Http\Requests\CreateStatisticsRequest;
use App\Http\Requests\UpdateStatisticsRequest;
use App\Models\Goods;
use App\Models\GoodsAttribute;
use App\Models\Statistics;
use App\Repositories\StatisticsRepository;
use App\Http\Controllers\AppBaseController;
use App\User;
use Illuminate\Http\Request;
use Flash;
use Illuminate\Support\Facades\DB;
use Prettus\Repository\Criteria\RequestCriteria;
use Response;

class StatisticsController extends AppBaseController
{
    /** @var  StatisticsRepository */
    private $statisticsRepository;

    public function __construct(StatisticsRepository $statisticsRepo)
    {
        $this->statisticsRepository = $statisticsRepo;
    }

    /**
     * Display a listing of the Statistics.
     *
     * @param Request $request
     * @return Response
     */
    public function index(Request $request)
    {
        $statistics = Statistics::select(
            'id',
            'type',
            'date',
            'add_users_count',
            'users_count',
            'goods_count',
            'orders_count',
            'orders_add_count',
            'orders_add_pay_count',
            'orders_return_count',
            'orders_return_amount',
            'orders_return_rate',
            'repurchase_count',
            'repurchase_rate',
            'orders_price_amount',
            'orders_pay_money_amount'
        )->orderBy('date','desc')->paginate(20);
        return view('statistics.index')
            ->with('statistics', $statistics);
    }

    /**
     * Show the form for creating a new Statistics.
     *
     * @return Response
     */
    public function create()
    {
        return view('statistics.create');
    }

    /**
     * Store a newly created Statistics in storage.
     *
     * @param CreateStatisticsRequest $request
     *
     * @return Response
     */
    public function store(CreateStatisticsRequest $request)
    {
        $input = $request->all();

        $statistics = $this->statisticsRepository->create($input);

        Flash::success('Statistics saved successfully.');

        return redirect(route('statistics.index'));
    }

    /**
     * Display the specified Statistics.
     *
     * @param  int $id
     *
     * @return Response
     */
    public function show($id)
    {
        $statistics = $this->statisticsRepository->findWithoutFail($id);

        if (empty($statistics)) {
            Flash::error('Statistics not found');

            return redirect(route('statistics.index'));
        }
        $statistics->info = json_decode($statistics->json,true);
        $statistics->users = json_decode($statistics->json2,true);
        $goods_arr = [];
        $goods = Goods::whereIn('id',$statistics->info['goods_ids'])->get();
        foreach ($goods as $v) {
            $goods_arr[$v->id] = $v;
        }

        $attribute_arr = [];
        $attributes = GoodsAttribute::whereIn('id',$statistics->info['attributes_ids'])->get();
        foreach ($attributes as $v) {
            $attribute_arr[$v->id] = $v;
        }

        $user_arr = [];
        $users = DB::table('users')->whereIn('id',$statistics->info['users_ids'])->get();
//        $users = User::all();
        foreach ($users as $v) {
            $user_arr[$v->id] = $v;
        }
        return view('statistics.show')->with('statistics', $statistics)->with('attribute_arr', $attribute_arr)
            ->with('goods_arr', $goods_arr)->with('user_arr', $user_arr);
    }

    /**
     * Show the form for editing the specified Statistics.
     *
     * @param  int $id
     *
     * @return Response
     */
    public function edit($id)
    {
        $statistics = $this->statisticsRepository->findWithoutFail($id);

        if (empty($statistics)) {
            Flash::error('Statistics not found');

            return redirect(route('statistics.index'));
        }

        return view('statistics.edit')->with('statistics', $statistics);
    }

    /**
     * Update the specified Statistics in storage.
     *
     * @param  int              $id
     * @param UpdateStatisticsRequest $request
     *
     * @return Response
     */
    public function update($id, UpdateStatisticsRequest $request)
    {
        $statistics = $this->statisticsRepository->findWithoutFail($id);

        if (empty($statistics)) {
            Flash::error('Statistics not found');

            return redirect(route('statistics.index'));
        }

        $statistics = $this->statisticsRepository->update($request->all(), $id);

        Flash::success('Statistics updated successfully.');

        return redirect(route('statistics.index'));
    }

    /**
     * Remove the specified Statistics from storage.
     *
     * @param  int $id
     *
     * @return Response
     */
    public function destroy($id)
    {
        $statistics = $this->statisticsRepository->findWithoutFail($id);

        if (empty($statistics)) {
            Flash::error('Statistics not found');

            return redirect(route('statistics.index'));
        }

        $this->statisticsRepository->delete($id);

        Flash::success('Statistics deleted successfully.');

        return redirect(route('statistics.index'));
    }

    public function refresh($id)
    {
        $statistics = $this->statisticsRepository->findWithoutFail($id);

        if (empty($statistics)) {
            Flash::error('Statistics not found');

            return redirect(route('statistics.index'));
        }

        $this->statisticsRepository->generate($statistics->type==1?1:30,$statistics->type,$statistics->date);

        Flash::success('Statistics updated successfully.');

        return redirect(route('statistics.index'));
    }
}
